﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Common.BLL;
using Yipusoft.Exercitation.Model;
using Yipusoft.Common.Model;

public partial class Combat_BaseSkillSeting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();

    }
    protected void ddlRank_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSelect();
    }
    private void Bind()
    {
        ddlRank.DataSource= CodeItem.GetList(" ParentID='ZXSBJX00'");
        ddlRank.DataTextField = "ItemName";
        ddlRank.DataValueField = "ItemID";
        ddlRank.DataBind();

        ddlRank.Items.Insert(0, new ListItem("全部警衔", "all"));

        ddlStation.DataSource = CodeItem.GetList(" Left(ItemID,4)='SBGW' Order by ItemID");
        ddlStation.DataTextField = "ItemName";
        ddlStation.DataValueField = "ItemID";
        ddlStation.DataBind();

        List<ItemSkillInfo> itemSkillList=  ItemSkill.GetList(" ParentID IN(Select ID From ItemSkill Where ParentID='55F5139F-489A-487D-8D55-FF6E555C7FDA')");
        foreach (ItemSkillInfo info in itemSkillList)
        {
            SkillList.Items.Add(new ListItem(info.ItemSkillName + "[" + info.AlisSkillName + "]", info.ID.ToString()));
        }        

    }
    private void BindSelect()
    {
        List<BaseSkillInfo2> skillList2 = new List<BaseSkillInfo2>();
        if (ddlRank.SelectedValue.Equals("all"))
        {
            List<BaseSkillInfo> skillList = BaseSkill.GetList(" StationID='" + ddlStation.SelectedValue + "'");
            
            BaseSkillInfo2 info2;
            foreach (BaseSkillInfo info in skillList)
            {
                if (skillList2.Where(o => o.ItemSkillID == info.ItemSkillID).Count() <= 0)
                {
                       List<CodeItemInfo> itemList= CodeItem.GetList(" ItemID Not IN (Select RankID From BaseSkill Where StationID='" + ddlStation.SelectedValue + "' And ItemSkillID='" + info.ItemSkillID + "') And ParentID='ZXSBJX00'");
                       int count = itemList != null ? itemList.Count : 0;
                      //  BaseSkill.GetCount(" RankID Not IN (Select ItemID From CodeItems Where  ParentID='ZXSBJX00') And StationID ='" + ddlStation.SelectedValue + "' And ItemSkillID='" + info.ItemSkillID + "'");

                    if (count <= 0)
                    {
                        info2 = new BaseSkillInfo2();
                        info2.ID = info.ID;
                        info2.RankID = info.RankID;
                        info2.ItemSkillID = info.ItemSkillID;
                        info2.StationID = info.StationID;
                        ItemSkillInfo itemInfo = ItemSkill.GetItemSkill(info.ItemSkillID);
                        info2.ItemSkillName = itemInfo.ItemSkillName + "[" + itemInfo.AlisSkillName + "]";
                        skillList2.Add(info2);
                    }
                }
            }
        }
        else
        {
            List<BaseSkillInfo> skillList = BaseSkill.GetList(" RankID='"+ddlRank.SelectedValue+"' And StationID='" + ddlStation.SelectedValue + "'");
            BaseSkillInfo2 info2;
            foreach (BaseSkillInfo info in skillList)
            {
                info2 = new BaseSkillInfo2();
                info2.ID = info.ID;
                info2.RankID = info.RankID;
                info2.ItemSkillID = info.ItemSkillID;
                info2.StationID = info.StationID;
                ItemSkillInfo itemInfo = ItemSkill.GetItemSkill(info.ItemSkillID);
                info2.ItemSkillName = itemInfo.ItemSkillName + "[" + itemInfo.AlisSkillName + "]";
                skillList2.Add(info2);
            }
        }

        SelectList.Items.Clear();
        SelectList.DataSource = skillList2;
        SelectList.DataTextField = "ItemSkillName";
        SelectList.DataValueField = "ID";
        SelectList.DataBind();      

    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        if (ddlStation.SelectedValue.Equals("SBGW0000"))
            return;

        if (ddlRank.SelectedValue.Equals("all"))
        {
           List<CodeItemInfo> itemList= CodeItem.GetList(" ParentID='ZXSBJX00' Order by ItemID");
           foreach (CodeItemInfo iteminfo in itemList)
           {
               foreach (ListItem item in SkillList.Items)
               {
                   if (item.Selected)
                   {
                       BaseSkillInfo info = new BaseSkillInfo();
                       info.ID = Guid.NewGuid();
                       info.ItemSkillID = new Guid(item.Value.ToString());
                       info.RankID = iteminfo.ItemID;
                       info.StationID = ddlStation.SelectedValue;
                       BaseSkill.Insert(info);
                   }
               }
           }
        }
        else
        {
            foreach (ListItem item in SkillList.Items)
            {
                if (item.Selected)
                {
                    BaseSkillInfo info = new BaseSkillInfo();
                    info.ID = Guid.NewGuid();
                    info.ItemSkillID = new Guid(item.Value.ToString());
                    info.RankID = ddlRank.SelectedValue;
                    info.StationID = ddlStation.SelectedValue;
                    BaseSkill.Insert(info);
                }
            }
        }
        BindSelect();
    }
    protected void ButtonRemove_Click(object sender, EventArgs e)
    {
        if (ddlRank.SelectedValue.Equals("all"))
        {
            foreach (ListItem item in SelectList.Items)
            {
                if (item.Selected)
                {
                    BaseSkill.Delete("ItemSkillID IN (Select ItemSkillID From BaseSkill Where ID='" + item.Value + "')");
                }
            }
        }
        else
        {
            foreach (ListItem item in SelectList.Items)
            {
                if (item.Selected)
                {
                    BaseSkill.Delete(new Guid(item.Value.ToString()));
                }
            }
        }
        BindSelect();
    }
}

public class BaseSkillInfo2 : BaseSkillInfo
{
    public string ItemSkillName
    {
        get;
        set;
    }
}
